package com.saodisheng.service.impl;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.saodisheng.model.NewsFeed;
import com.saodisheng.model.Posts;
import com.saodisheng.model.PostsComment;
import com.saodisheng.model.ReferMe;
import com.saodisheng.service.NewsFeedService;
import com.saodisheng.service.ReferMeService;
import com.saodisheng.service.RemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 提到我业务，分为 @我 以及评论/回复我
 *
 * @author 扫地生_saodisheng
 * @date 2022/1/28
 */
@Service
public class ReferMeServiceImpl implements ReferMeService {
	@Autowired
	private NewsFeedService newsFeedService;
	@Autowired
	private RemindService remindService;

	final String referMePageCacheName = "referMePage";
	final ReferMe dao = new ReferMe().dao();
	final NewsFeed newsFeedDao = new NewsFeed().dao();
	final int pageSize = 15;


	@Override
	public void createPostsReferMe(List<Integer> referAccounts, Integer newsFeedId, Posts post) {
		//针对posts中的@我   生成   refer_me
		createReferMe(referAccounts, newsFeedId, ReferMe.TYPE_AT_ME);
		clearCache();
	}
	
	@Override
	public void createPostsCommentReferMe(List<Integer> referAccounts, Integer newsFeedId, PostsComment comment) {
		//针对comment中的@我   生成   refer_me
		createReferMe(referAccounts, newsFeedId, ReferMe.TYPE_AT_ME);
		clearCache();
	}
	
	
	private void createReferMe(List<Integer> referAccounts, int newsFeedId, int type) {
		for (Integer referAccountId : referAccounts) {
			createReferMe(referAccountId, newsFeedId, type);
		}
	}

	/**
	 * 生成一条 refer_me，同时也生成一条 remind 提醒用户
	 */
	private void createReferMe(int referAccountId, int newsFeedId, int type) {
		ReferMe rm = new ReferMe();
		// 接收者
		rm.setReferAccountId(referAccountId);
		// newsFeedId
		rm.setNewsFeedId(newsFeedId);
		// @我 类型的 referType
		rm.setType(type);
		rm.setCreateAt(new Date());
		rm.save();

		// 每一条 refer_me 都创建 remind 提醒
		remindService.createRemindOfReferMe(referAccountId);
	}
	

	@Override
	public Page<NewsFeed> paginate(int accountId, int pageNum) {
		String cacheKey = accountId + "_" + pageNum;
		Page<NewsFeed> newsFeedPage = CacheKit.get(referMePageCacheName, cacheKey);
		if (newsFeedPage == null) {
			// 先获取 refer_me 的数据
			String s = "select newsFeedId";
			String f = "from refer_me where referAccountId=? order by id desc";
			Page<ReferMe> referMePage = dao.paginate(pageNum, pageSize, s, f, accountId);
			if (referMePage.getList().size() == 0) {
				newsFeedPage =  new Page<NewsFeed>(new ArrayList<NewsFeed>(), pageNum, pageSize, 0, 0);
				CacheKit.put(referMePageCacheName, cacheKey, newsFeedPage);
				return newsFeedPage;
			}

            // TODO 将来用 accountService.join(...) 代替 inner join 关联操作
			StringBuilder sql = new StringBuilder("select nf.*, a.avatar, a.nickName ");
			sql.append("from news_feed nf inner join user a on nf.accountId=a.id where nf.id in(");
			appendNewsFeedIds(referMePage.getList(), sql);
			sql.append(") order by id desc");

			List<NewsFeed> newsFeedList = newsFeedDao.find(sql.toString());
			newsFeedPage = new Page<NewsFeed>(newsFeedList, pageNum, pageSize, referMePage.getTotalPage(), referMePage.getTotalRow());

			// 重用 NewsFeed 模块的 loadRefData 功能
			newsFeedService.loadRefData(newsFeedPage);
			CacheKit.put(referMePageCacheName, cacheKey, newsFeedPage);
		}
		return newsFeedPage;
	}
	
	private void appendNewsFeedIds(List<ReferMe> list, StringBuilder ret) {
		for (int i=0, size=list.size(); i<size; i++) {
			if (i > 0) {
				ret.append(", ");
			}
			ret.append(list.get(i).getNewsFeedId());
		}
	}
	
	@Override
	public void clearCache() {
		CacheKit.removeAll(referMePageCacheName);
	}
}
